//
//  ViewpointDetailCell.m
//  FloatClouds
//
//  Created by 黄威 on 16/9/8.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ViewpointDetailCell.h"

@implementation ViewpointDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.labelOfTitle = [[BaseLabel alloc]init];
        self.labelOfTitle.font = [UIFont fontWithName:@"Helvetica" size:19];
        self.labelOfTitle.tintColor = [UIColor grayColor];
        self.labelOfTitle.numberOfLines = 0;
        self.labelOfTitle.textAlignment = NSTextAlignmentLeft;
        self.labelOfTitle.frame = CGRectMake(10, 5, [UIScreen mainScreen].bounds.size.width, 60);
        [self.contentView addSubview:self.labelOfTitle];
        
        
        
        self.labelOfTime = [[BaseLabel alloc]init];
        self.labelOfTime.font = [UIFont fontWithName:@"Helvetica" size:15];
        self.labelOfTime.tintColor = [UIColor grayColor];
        [self.contentView addSubview:self.labelOfTime];
        self.labelOfTime.frame = CGRectMake(10, 65, self.bounds.size.width * 0.3, 15);
        
        self.labelOfCome_From = [[BaseLabel alloc]init];
        self.labelOfCome_From.font = [UIFont fontWithName:@"Helvetica" size:15];
        self.labelOfCome_From.tintColor = [UIColor grayColor];
        [self.contentView addSubview:self.labelOfCome_From];
        self.labelOfCome_From.frame = CGRectMake(self.bounds.size.width *0.4, 65, self.bounds.size.width *0.3, 15);
        
        
        self.webView = [[UIWebView alloc]init];
        self.webView.frame = CGRectMake(0, 80,[[UIScreen mainScreen] bounds].size.width, 1);
        self.webView.delegate = self;
        self.webView.backgroundColor = [UIColor clearColor];
        self.webView.opaque = NO;
        self.webView.userInteractionEnabled = NO;
        self.webView.scrollView.bounces = NO;
        self.webView.scalesPageToFit = YES;
        [self addSubview:self.webView];
        
      
    }

    return self;


}


-(void)setContentStr:(NSString *)contentStr
{
    _contentStr = contentStr;
    
    
    NSString *htmls = [NSString stringWithFormat:@"<html> \n"
                       "<head> \n"
                       "<style type=\"text/css\"> \n"
                       "body {font-size:40px;}\n"
                       "</style> \n"
                       "</head> \n"
                       "<body>"
                       "<script type='text/javascript'>"
                       "window.onload = function(){\n"
                       "var $img = document.getElementsByTagName('img');\n"
                       "for(var p in  $img){\n"
                       " $img[p].style.width = '100%%';\n"
                       "$img[p].style.height ='auto'\n"
                       "}\n"
                       "}"
                       "</script>%@"
                       "</body>"
                       "</html>",_contentStr];
  
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self.webView loadHTMLString:htmls baseURL:nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
        });
        
    });

    
    
}



#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 如果要获取web高度必须在网页加载完成之后获取
    
    // 方法一
    //CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero];
    
    // 方法二
        CGSize fittingSize = webView.scrollView.contentSize;
   
    self.height = fittingSize.height;
    
    self.webView.frame = CGRectMake(0, 85, fittingSize.width, fittingSize.height * 2 );
    
    
//    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
//    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
//    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//    
    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"WEBVIEW_HEIGHT" object:self userInfo:nil];

    
    // 用通知发送加载完成后的高度
    }


-(void)viewDidDisappear:(BOOL)animated {   [self.webView removeFromSuperview]; self.webView = nil; self.webView.delegate = nil; [self.webView stopLoading]; }


- (void)layoutSubviews{

    [super layoutSubviews];


}





@end
